using Microsoft.AspNetCore.Mvc;
using UniversalAdminSystem.Application.SystemSettings.DTOs;
using UniversalAdminSystem.Application.SystemSettings.Interfaces;
using UniversalAdminSystem.Api.Attributes;
using UniversalAdminSystem.Application.Common.Results;

namespace UniversalAdminSystem.Api.Controllers;

[ApiController]
[Route("api/system-settings")]
public class SystemSettingsController : ControllerBase
{
    private readonly ISystemSettingAppService _settingService;
    public SystemSettingsController(ISystemSettingAppService settingService) => _settingService = settingService;

    [HttpGet]
    //[RequirePermission("systemsetting:read")]
    public async Task<IActionResult> GetAllSettingsAsync()
    {
        var result = await _settingService.GetAllSettingsAsync();
        return result.IsSuccess ? Ok(result) : BadRequest(result);
    }

    [HttpGet("key/{key}")]
    //[RequirePermission("systemsetting:read")]
    public async Task<IActionResult> GetSettingByKeyAsync(string key)
    {
        var result = await _settingService.GetSettingByKeyAsync(key);
        return result.IsSuccess ? Ok(result) : BadRequest(result);
    }

    [HttpGet("group/{group}")]
    //[RequirePermission("systemsetting:read")]
    public async Task<IActionResult> GetSettingsByGroupAsync(string group)
    {
        var result = await _settingService.GetSettingsByGroupAsync(group);
        return result.IsSuccess ? Ok(result) : BadRequest(result);
    }

    [HttpPost]
    //[RequirePermission("systemsetting:create")]
    public async Task<IActionResult> CreateSettingAsync([FromBody] SystemSettingCreateDto dto)
    {
        var result = await _settingService.CreateSettingAsync(dto);
        return result.IsSuccess ? Ok(result) : BadRequest(result);
    }

    [HttpPut("{id}")]
    //[RequirePermission("systemsetting:update")]
    public async Task<IActionResult> UpdateSetting(Guid id, [FromBody] SystemSettingUpdateDto updateDto)
    {
        var result = await _settingService.UpdateSettingAsync(id, updateDto);
        return result.IsSuccess ? Ok(result) : BadRequest(result);
    }

    [HttpDelete("{id}")]
    //[RequirePermission("systemsetting:delete")]
    public async Task<IActionResult> DeleteSettingAsync(Guid id)
    {
        var result = await _settingService.DeleteSettingAsync(id);
        return result.IsSuccess ? Ok(result) : BadRequest(result);
    }
}